//index.js
//获取应用实例
const app = getApp()

var {Orienter} = require('../../utils/orienter.js')

Page({
  data: {
    transformX: -10,
    transformY: -10
  },
  onLoad: function () {
    this.os = app.globalData.systemInfo.platform;
    console.log(this.os);
  },
  onShow: function () {
    var that = this;
    var o = new Orienter();
    o.os = app.globalData.systemInfo.platform;
    o.onOrient = function (obj) {
      var a, b;
      a = obj.lon < 180 ? obj.lon : obj.lon - 360;
      console.log('a, b: ', a, b);
      a = a > 0 ? a > 20 ? 0 : a : a < -20 ? -20 : a;
      b = b > 0 ? b > -180 ? 0 : b : b < -160 ? -20 : b;

      // console.log([a, b]);
      that.setData({
        transformX: a,
        transformY: b
      })
    };
    wx.startDeviceMotionListening({
      success: function (res) {
        console.log("startDeviceMotionListening:", res);
      }
    })
    wx.onDeviceMotionChange(res => {
      o._orient(res)
    })
  },
  onUnload: function() {
    wx.stopDeviceMotionListening({
      success: function (res) {
        console.log("stopDeviceMotionListening:", res);
      }
    })
    wx.offDeviceMotionChange({
      success: function (res) {
        console.log("offDeviceMotionChange:", res);
      }
    })
  },
})
